<template>
  <view class="u-upload custom" v-if="!disabled">
    <view
      v-if="showUploadList"
      class="u-list-item u-preview-wrap"
      v-for="(item, index) in lists"
      :key="index"
      :style="{
        width: $u.addUnit(width),
        height: $u.addUnit(height)
      }"
    >
      <view
        v-if="deletable"
        class="u-delete-icon"
        @tap.stop="deleteItem(index)"
        :style="{
          background: delBgColor
        }"
      >
        <u-icon
          class="u-icon"
          :name="delIcon"
          size="20"
          :color="delColor"
        ></u-icon>
      </view>
      <!-- <view
				v-if="item.progress >= 100"
				class="u-success-icon"
			>
				<u-icon class="u-icon" :name="successIcon" size="20" :color="successColor"></u-icon>
			</view> -->
      <u-line-progress
        v-if="
          showProgress &&
          item.progress > 0 &&
          item.progress != 100 &&
          !item.error
        "
        :show-percent="false"
        height="16"
        class="u-progress"
        :percent="item.progress"
      ></u-line-progress>
      <view @tap.stop="retry(index)" v-if="item.error" class="u-error-btn"
        >点击重试</view
      >
      <image
        @tap.stop="doPreviewImage(item.url || item.path, index)"
        class="u-preview-image"
        v-if="!item.isImage"
        :src="item.url || item.path"
        :mode="imageMode"
      ></image>
    </view>
    <slot name="file" :file="lists"></slot>
    <view
      style="display: inline-block"
      @tap="selectFile"
      v-if="maxCount > lists.length"
    >
      <slot name="addBtn"></slot>
      <view
        v-if="!customBtn"
        class="u-list-item u-add-wrap"
        hover-class="u-add-wrap__hover"
        hover-stay-time="150"
        :style="{
          width: $u.addUnit(width),
          height: $u.addUnit(height)
        }"
      >
        <u-icon name="plus" class="u-add-btn" size="40"></u-icon>
        <view class="u-add-tips" v-if="uploadText">{{ uploadText }}</view>
      </view>
    </view>
  </view>
</template>

<script>
import { ALI_OSS_ENDPOINT } from '@/common/config.ts'
import { getFormDataParams, getAliOss } from '@/common/ali-oss.ts'
import { generateId, get_suffix } from '@/utils/common.ts'
import { imageUrlToFile } from '@/utils/common.ts'
/**
 * upload 图片上传
 * @description 该组件用于上传图片场景
 * @tutorial https://www.uviewui.com/components/upload.html
 * @property {String} action 服务器上传地址
 * @property {String Number} max-count 最大选择图片的数量（默认99）
 * @property {Boolean} custom-btn 如果需要自定义选择图片的按钮，设置为true（默认false）
 * @property {Boolean} show-progress 是否显示进度条（默认true）
 * @property {Boolean} disabled 是否启用(显示/移仓)组件（默认false）
 * @property {String} image-mode 预览图片等显示模式，可选值为uni的image的mode属性值（默认aspectFill）
 * @property {String} del-icon 右上角删除图标名称，只能为uView内置图标
 * @property {String} del-bg-color 右上角关闭按钮的背景颜色
 * @property {String | Number} index 在各个回调事件中的最后一个参数返回，用于区别是哪一个组件的事件
 * @property {String} del-color 右上角关闭按钮图标的颜色
 * @property {Object} header 上传携带的头信息，对象形式
 * @property {Object} form-data 上传额外携带的参数
 * @property {String} name 上传文件的字段名，供后端获取使用（默认file）
 * @property {Array<String>} size-type original 原图，compressed 压缩图，默认二者都有（默认['original', 'compressed']）
 * @property {Array<String>} source-type 选择图片的来源，album-从相册选图，camera-使用相机，默认二者都有（默认['album', 'camera']）
 * @property {Boolean} preview-full-image	是否可以通过uni.previewImage预览已选择的图片（默认true）
 * @property {Boolean} multiple	是否开启图片多选，部分安卓机型不支持（默认true）
 * @property {Boolean} deletable 是否显示删除图片的按钮（默认true）
 * @property {String Number} max-size 选择单个文件的最大大小，单位B(byte)，默认不限制（默认Number.MAX_VALUE）
 * @property {Array<Object>} file-list 默认显示的图片列表，数组元素为对象，必须提供url属性
 * @property {Boolean} upload-text 选择图片按钮的提示文字（默认“选择图片”）
 * @property {Boolean} auto-upload 选择完图片是否自动上传，见上方说明（默认true）
 * @property {Boolean} show-tips 特殊情况下是否自动提示toast，见上方说明（默认true）
 * @property {Boolean} show-upload-list 是否显示组件内部的图片预览（默认true）
 * @event {Function} on-oversize 图片大小超出最大允许大小
 * @event {Function} on-preview 全屏预览图片时触发
 * @event {Function} on-remove 移除图片时触发
 * @event {Function} on-success 图片上传成功时触发
 * @event {Function} on-change 图片上传后，无论成功或者失败都会触发
 * @event {Function} on-error 图片上传失败时触发
 * @event {Function} on-progress 图片上传过程中的进度变化过程触发
 * @event {Function} on-uploaded 所有图片上传完毕触发
 * @event {Function} on-choose-complete 每次选择图片后触发，只是让外部可以得知每次选择后，内部的文件列表
 * @event {Function} on-list-change 当内部文件列表被加入文件、移除文件，或手动调用clear方法时触发
 * @event {Function} on-choose-fail 选择文件出错时触发，比如选择文件时取消了操作，只在微信和APP有效
 * @example <u-upload :action="action" :file-list="fileList" ></u-upload>
 */
export default {
  name: 'u-upload',
  emits: [
    'update:file-list',
    'on-oversize',
    'on-list-change',
    'on-preview',
    'on-remove',
    'on-success',
    'on-change',
    'on-error',
    'on-progress',
    'on-uploaded',
    'on-choose-complete',
    'on-choose-fail'
  ],
  props: {
    //是否显示组件自带的图片预览功能
    showUploadList: {
      type: Boolean,
      default: true
    },
    // 后端地址
    action: {
      type: String,
      default: ''
    },
    // 最大上传数量
    maxCount: {
      type: [String, Number],
      default: 52
    },
    //  是否显示进度条
    showProgress: {
      type: Boolean,
      default: true
    },
    // 是否启用
    disabled: {
      type: Boolean,
      default: false
    },
    // 预览上传的图片时的裁剪模式，和image组件mode属性一致
    imageMode: {
      type: String,
      default: 'aspectFill'
    },
    // 头部信息
    header: {
      type: Object,
      default() {
        return {}
      }
    },
    // 额外携带的参数
    formData: {
      type: Object,
      default() {
        return {}
      }
    },
    // 上传的文件字段名
    name: {
      type: String,
      default: 'file'
    },
    // 所选的图片的尺寸, 可选值为original compressed
    sizeType: {
      type: Array,
      default() {
        return ['original', 'compressed']
      }
    },
    sourceType: {
      type: Array,
      default() {
        return ['album', 'camera']
      }
    },
    // 是否在点击预览图后展示全屏图片预览
    previewFullImage: {
      type: Boolean,
      default: true
    },
    // 是否开启图片多选，部分安卓机型不支持
    multiple: {
      type: Boolean,
      default: true
    },
    // 是否展示删除按钮
    deletable: {
      type: Boolean,
      default: true
    },
    // 文件大小限制，单位为byte
    maxSize: {
      type: [String, Number],
      default: 5 * 1024 * 1024 //5M
      // default: Number.MAX_VALUE
    },
    // 显示已上传的文件列表
    fileList: {
      type: Array,
      default() {
        return []
      }
    },
    // 上传区域的提示文字
    uploadText: {
      type: String,
      default: '选择图片'
    },
    // 是否自动上传
    autoUpload: {
      type: Boolean,
      default: true
    },
    // 是否显示toast消息提示
    showTips: {
      type: Boolean,
      default: true
    },
    // 是否通过slot自定义传入选择图标的按钮
    customBtn: {
      type: Boolean,
      default: false
    },
    // 内部预览图片区域和选择图片按钮的区域宽度
    width: {
      type: [String, Number],
      default: 200
    },
    // 内部预览图片区域和选择图片按钮的区域高度
    height: {
      type: [String, Number],
      default: 200
    },
    // 右上角关闭按钮的背景颜色
    delBgColor: {
      type: String,
      default: '#fa3534'
    },
    // 右上角关闭按钮的叉号图标的颜色
    delColor: {
      type: String,
      default: '#ffffff'
    },
    // 右上角删除图标名称，只能为uView内置图标
    delIcon: {
      type: String,
      default: 'close'
    },
    // 右下角成功图标名称，只能为uView内置图标
    successIcon: {
      type: String,
      default: 'checkbox-mark'
    },
    // 右下角成功的叉号图标的颜色
    successColor: {
      type: String,
      default: '#ffffff'
    },
    // 如果上传后的返回值为json字符串，是否自动转json
    toJson: {
      type: Boolean,
      default: true
    },
    // 上传前的钩子，每个文件上传前都会执行
    beforeUpload: {
      type: Function,
      default: null
    },
    // 移除文件前的钩子
    beforeRemove: {
      type: Function,
      default: null
    },
    // 允许上传的图片后缀
    limitType: {
      type: Array,
      default() {
        // 支付宝小程序真机选择图片的后缀为"image"
        // https://opendocs.alipay.com/mini/api/media-image
        return ['png', 'jpg', 'jpeg', 'webp', 'gif', 'image']
      }
    },
    // 在各个回调事件中的最后一个参数返回，用于区别是哪一个组件的事件
    index: {
      type: [Number, String],
      default: ''
    }
  },
  mounted() {},
  data() {
    return {
      lists: [],
      isInCount: true,
      uploading: false
    }
  },
  watch: {
    fileList: {
      immediate: true,
      handler(val) {
        let that = this
        let lists = JSON.parse(JSON.stringify(that.lists))
        val.map((value) => {
          // 首先检查内部是否已经添加过这张图片，因为外部绑定了一个对象给fileList的话(对象引用)，进行修改外部fileList
          // 时，会触发watch，导致重新把原来的图片再次添加到this.lists
          // 数组的some方法意思是，只要数组元素有任意一个元素条件符合，就返回true，而另一个数组的every方法的意思是数组所有元素都符合条件才返回true
          let tmp = lists.some((val) => {
            return val.url == value.url
          })
          // 如果内部没有这个图片(tmp为false)，则添加到内部
          if (!tmp) {
            lists.push({ url: value.url, error: false, progress: 100 })
          }
        })
        that.lists = JSON.parse(JSON.stringify(lists))
      }
    },
    // 监听lists的变化，发出事件
    lists: {
      deep: true,
      handler(n) {
        this.$emit('update:file-list', n)
        this.$emit('on-list-change', n, this.index)
      }
    }
  },
  methods: {
    // 清除列表
    clear() {
      this.lists = []
    },
    // 重新上传队列中上传失败的所有文件
    reUpload() {
      this.uploadFile()
    },
    // 选择图片
    selectFile2() {
      console.log('点击了选择按钮')
      let that = this
      if (that.disabled) return
      const {
        name = '',
        maxCount,
        multiple,
        maxSize,
        sizeType,
        camera,
        compressed,
        maxDuration,
        sourceType
      } = that
      let chooseFile = null
      let lists = JSON.parse(JSON.stringify(that.lists))
      const newMaxCount = maxCount - lists.length
      // 设置为只选择图片的时候使用 chooseImage 来实现
      console.log('进入choose前')
      try {
        console.log('进入choose后')
        chooseFile = new Promise((resolve, reject) => {
          uni.chooseImage({
            count: multiple ? (newMaxCount > 9 ? 9 : newMaxCount) : 1,
            sourceType: sourceType,
            sizeType,
            success: resolve,
            fail: reject
          })
        })
        chooseFile
          .then((res) => {
            console.log('上传图片结果', res)
            let file = null
            let listOldLength = that.lists.length
            res.tempFiles.map((val, index) => {
              // 检查文件后缀是否允许，如果不在that.limitType内，就会返回false
              if (!that.checkFileExt(val)) return

              // 如果是非多选，index大于等于1或者超出最大限制数量时，不处理
              if (!multiple && index >= 1) return
              if (val.size > maxSize) {
                that.$emit('on-oversize', val, that.lists, that.index)
                that.showToast('超出允许的文件大小')
              } else {
                if (maxCount <= lists.length) {
                  that.$emit('on-exceed', val, that.lists, that.index)
                  that.showToast('超出最大允许的文件个数')
                  return
                }
                lists.push({
                  url: val.path,
                  progress: 0,
                  error: false,
                  file: val
                })
              }
            })

            // 这样实现深拷贝会导致在H5中file为空对象
            // that.lists = JSON.parse(JSON.stringify(lists));
            this.deepClone(lists, that.lists)
            // 每次图片选择完，抛出一个事件，并将当前内部选择的图片数组抛出去
            that.$emit('on-choose-complete', that.lists, that.index)
            if (that.autoUpload) that.uploadFile(listOldLength)
          })
          .catch((error) => {
            uni.showToast({
              title: `选择图片错误${error}`,
              icon: 'none'
            })
            that.$emit('on-choose-fail', error)
          })
      } catch (err) {
        uni.showToast({
          title: `拉不起来的`,
          icon: 'none'
        })
        console.log('拉不起来的', err)
      }
    },
    selectFile3() {
      uni.showToast({
        title: `点击了选择按钮`,
        icon: 'none'
      })
      let that = this
      if (that.disabled) return
      // 新增：权限检查逻辑
      const checkAndroidPermissions = async () => {
        // #ifdef APP-PLUS
        // 引入uni的权限插件
        const permision = uni.requireNativePlugin('system.permission')
        // 需要申请的权限列表
        const permissions = [
          'android.permission.READ_EXTERNAL_STORAGE',
          'android.permission.WRITE_EXTERNAL_STORAGE'
        ]
        // 如果需要相机，添加相机权限
        if (that.sourceType.includes('camera')) {
          permissions.push('android.permission.CAMERA')
        }
        // 检查是否已授权
        const hasAllPermissions = permissions.every((perm) =>
          permision.hasPermission(perm)
        )
        if (!hasAllPermissions) {
          // 申请权限
          const result = await permision.requestPermissions(permissions)
          // 检查是否有未通过的权限
          const denied = result.some((item) => !item.granted)
          if (denied) {
            uni.showModal({
              title: '权限不足',
              content: '需要相册和相机权限才能上传图片，请前往设置开启',
              confirmText: '去设置',
              success: (res) => {
                if (res.confirm) {
                  // 打开应用权限设置页
                  permision.gotoAppPermissionSetting()
                }
              }
            })
            return false
          }
        }
        // #endif
        return true
      }
      // 先检查权限，再执行选择图片逻辑
      checkAndroidPermissions().then((hasPermission) => {
        if (!hasPermission) return
        // 以下为原有选择图片逻辑（不变）
        const {
          name = '',
          maxCount,
          multiple,
          maxSize,
          sizeType,
          camera,
          compressed,
          maxDuration,
          sourceType
        } = that
        let chooseFile = null
        let lists = JSON.parse(JSON.stringify(that.lists))
        const newMaxCount = maxCount - lists.length
        uni.showToast({
          title: `进入choose前`,
          icon: 'none'
        })
        try {
          uni.showToast({
            title: `进入choose后`,
            icon: 'none'
          })
          chooseFile = new Promise((resolve, reject) => {
            uni.chooseImage({
              count: multiple ? (newMaxCount > 9 ? 9 : newMaxCount) : 1,
              sourceType: sourceType,
              sizeType,
              success: resolve,
              fail: reject
            })
          })
          chooseFile
            .then((res) => {
              uni.showToast({
                title: `上传图片结果${res}`,
                icon: 'none',
                duration: 6000
              })
              let file = null
              let listOldLength = that.lists.length
              res.tempFiles.map((val, index) => {
                // 检查文件后缀是否允许，如果不在that.limitType内，就会返回false
                if (!that.checkFileExt(val)) return

                // 如果是非多选，index大于等于1或者超出最大限制数量时，不处理
                if (!multiple && index >= 1) return
                if (val.size > maxSize) {
                  that.$emit('on-oversize', val, that.lists, that.index)
                  that.showToast('超出允许的文件大小')
                } else {
                  if (maxCount <= lists.length) {
                    that.$emit('on-exceed', val, that.lists, that.index)
                    that.showToast('超出最大允许的文件个数')
                    return
                  }
                  lists.push({
                    url: val.path,
                    progress: 0,
                    error: false,
                    file: val
                  })
                }
              })

              // 这样实现深拷贝会导致在H5中file为空对象
              // that.lists = JSON.parse(JSON.stringify(lists));
              this.deepClone(lists, that.lists)
              // 每次图片选择完，抛出一个事件，并将当前内部选择的图片数组抛出去
              that.$emit('on-choose-complete', that.lists, that.index)
              if (that.autoUpload) that.uploadFile(listOldLength)
            })
            .catch((error) => {
              console.error('选择图片失败：', error) // 新增详细错误打印
              uni.showToast({
                title: `选择失败：${error}`,
                icon: 'none',
                duration: 15000
              })
              that.$emit('on-choose-fail', error)
            })
        } catch (err) {
          console.error('选择图片异常：', err) // 新增详细错误打印
          uni.showToast({
            title: `选择异常：${err}`,
            icon: 'none',
            duration: 15000
          })
        }
      })
    },
    selectFile4() {
      let that = this
      if (that.disabled) return

      // 统一处理选择图片逻辑
      const handleChooseImage = async () => {
        const {
          name = '',
          maxCount,
          multiple,
          maxSize,
          sizeType,
          camera,
          compressed,
          maxDuration,
          sourceType
        } = that

        let lists = JSON.parse(JSON.stringify(that.lists))
        const newMaxCount = maxCount - lists.length
        uni.showToast({
          title: `进入choose前`,
          icon: 'none'
        })
        try {
          uni.showToast({
            title: `进入choose后`,
            icon: 'none'
          })
          // 直接调用选择图片API，不再使用Promise包装
          const res = await uni.chooseImage({
            count: multiple ? (newMaxCount > 9 ? 9 : newMaxCount) : 1,
            sourceType: sourceType,
            sizeType: sizeType
          })
          uni.showToast({
            title: `上传图片结果${res}`,
            icon: 'none',
            duration: 6000
          })
          let listOldLength = that.lists.length
          res.tempFiles.forEach((val, index) => {
            if (!that.checkFileExt(val)) return
            if (!multiple && index >= 1) return

            if (val.size > maxSize) {
              that.$emit('on-oversize', val, that.lists, that.index)
              that.showToast('超出允许的文件大小')
            } else {
              if (maxCount <= lists.length) {
                that.$emit('on-exceed', val, that.lists, that.index)
                that.showToast('超出最大允许的文件个数')
                return
              }
              lists.push({
                url: val.path,
                progress: 0,
                error: false,
                file: val
              })
            }
          })

          that.deepClone(lists, that.lists)
          that.$emit('on-choose-complete', that.lists, that.index)
          if (that.autoUpload) that.uploadFile(listOldLength)
        } catch (error) {
          uni.showToast({
            title: `选择失败：${error}`,
            icon: 'none',
            duration: 15000
          })
          console.error('选择图片失败：', error)
          that.$emit('on-choose-fail', error)
          // 安卓特有错误处理
          // #ifdef APP-PLUS
          if (error.errMsg && error.errMsg.indexOf('permission') !== -1) {
            that.showToast('需要相册和相机权限才能上传图片')
          } else {
            that.showToast('选择图片失败，请重试')
          }
          // #endif
        }
      }

      // 安卓权限检查
      // #ifdef APP-PLUS
      const checkPermissions = async () => {
        uni.showToast({
          title: `进入检查权限`,
          icon: 'none'
        })
        try {
          const permision = uni.requireNativePlugin('system.permission')
          const permissions = [
            'android.permission.READ_EXTERNAL_STORAGE',
            'android.permission.WRITE_EXTERNAL_STORAGE'
          ]

          if (that.sourceType.includes('camera')) {
            permissions.push('android.permission.CAMERA')
          }

          // 检查并请求权限
          for (const perm of permissions) {
            const hasPerm = await new Promise((resolve) => {
              permision.hasPermission(perm, (result) => {
                resolve(result)
              })
            })

            if (!hasPerm) {
              const granted = await new Promise((resolve) => {
                permision.requestPermission(perm, (result) => {
                  resolve(result === 1)
                })
              })

              if (!granted) {
                that.showToast('权限被拒绝，无法选择图片')
                return false
              }
            }
          }
          return true
        } catch (e) {
          uni.showToast({
            title: `权限检查错误${e}`,
            icon: 'none',
            duration: 12000
          })
          console.error('权限检查错误:', e)
          return true // 如果权限检查出错，仍然尝试继续
        }
      }

      // 安卓设备先检查权限
      checkPermissions().then((hasPermission) => {
        uni.showToast({
          title: `先检查权限`,
          icon: 'none'
        })
        if (hasPermission) {
          handleChooseImage()
        } else {
          // 提示用户去设置
          uni.showModal({
            title: '权限不足',
            content: '需要相册和相机权限才能上传图片，请前往设置开启',
            confirmText: '去设置',
            success: (res) => {
              if (res.confirm) {
                const permision = uni.requireNativePlugin('system.permission')
                permision.gotoAppPermissionSetting()
              }
            }
          })
        }
      })
      // #endif

      // 非安卓设备直接调用
      // #ifndef APP-PLUS
      handleChooseImage()
      // #endif
    },
    selectFile5() {
      uni.showToast({
        title: `点击了选择按钮`,
        icon: 'none'
      })
      let that = this
      if (that.disabled) return

      // 统一处理选择图片逻辑
      const handleChooseImage = async () => {
        const {
          name = '',
          maxCount,
          multiple,
          maxSize,
          sizeType,
          camera,
          compressed,
          maxDuration,
          sourceType
        } = that

        let lists = JSON.parse(JSON.stringify(that.lists))
        const newMaxCount = maxCount - lists.length
        uni.showToast({
          title: `进入choose前`,
          icon: 'none'
        })
        try {
          // 安卓WebView特殊处理：尝试两种调用方式
          let res
          try {
            // 方式1：直接调用uni.chooseImage
            res = await uni.chooseImage({
              count: multiple ? (newMaxCount > 9 ? 9 : newMaxCount) : 1,
              sourceType: sourceType,
              sizeType: sizeType
            })
            uni.showToast({
              title: `使用方式1`,
              icon: 'none'
            })
          } catch (error) {
            uni.showToast({
              title: `方式1失败，尝试方式2${error}`,
              icon: 'none'
            })
            console.log('方式1失败，尝试方式2', error)

            // 方式2：通过创建临时按钮触发选择
            res = await that.chooseImageViaButton()
          }

          let listOldLength = that.lists.length
          res.tempFiles.forEach((val, index) => {
            if (!that.checkFileExt(val)) return
            if (!multiple && index >= 1) return

            if (val.size > maxSize) {
              that.$emit('on-oversize', val, that.lists, that.index)
              that.showToast('超出允许的文件大小')
            } else {
              if (maxCount <= lists.length) {
                that.$emit('on-exceed', val, that.lists, that.index)
                that.showToast('超出最大允许的文件个数')
                return
              }
              lists.push({
                url: val.path,
                progress: 0,
                error: false,
                file: val
              })
            }
          })

          that.deepClone(lists, that.lists)
          that.$emit('on-choose-complete', that.lists, that.index)
          if (that.autoUpload) that.uploadFile(listOldLength)
        } catch (error) {
          uni.showToast({
            title: `选择图片失败：${error}`,
            icon: 'none'
          })
          console.error('选择图片失败：', error)
          that.$emit('on-choose-fail', error)
          that.showToast('选择图片失败，请重试')
        }
      }

      // 安卓WebView特殊处理：通过创建临时按钮触发选择
      const chooseImageViaButton = () => {
        return new Promise((resolve, reject) => {
          try {
            uni.showToast({
              title: `创建`,
              icon: 'none'
            })
            // 创建临时input元素
            const input = document.createElement('input')
            input.type = 'file'
            input.accept = 'image/*'
            input.style.display = 'none'

            // 设置选择数量限制
            if (this.multiple) {
              input.multiple = true
              // 最多可以选择9张或剩余可上传数量
              const maxFiles = Math.min(this.maxCount - this.lists.length, 9)
              if (maxFiles > 1) {
                input.setAttribute('max', maxFiles)
              }
            }

            // 处理文件选择
            input.onchange = (e) => {
              const files = Array.from(e.target.files || [])
              if (files.length === 0) {
                reject(new Error('未选择文件'))
                return
              }

              const tempFiles = files.map((file) => ({
                path: URL.createObjectURL(file),
                size: file.size,
                name: file.name,
                type: file.type
              }))

              resolve({ tempFiles })
              document.body.removeChild(input)
            }

            // 添加到DOM并触发点击
            document.body.appendChild(input)
            input.click()

            // 超时处理
            setTimeout(() => {
              if (document.body.contains(input)) {
                document.body.removeChild(input)
                reject(new Error('选择超时'))
              }
            }, 30000)
          } catch (error) {
            uni.showToast({
              title: `创建失败`,
              icon: 'none'
            })
            reject(error)
          }
        })
      }

      // #ifdef APP-PLUS
      // 安卓权限检查 - 仅限APP环境
      const checkPermissions = async () => {
        try {
          const permision = uni.requireNativePlugin('system.permission')
          const permissions = [
            'android.permission.READ_EXTERNAL_STORAGE',
            'android.permission.WRITE_EXTERNAL_STORAGE'
          ]

          if (that.sourceType.includes('camera')) {
            permissions.push('android.permission.CAMERA')
          }

          // 检查并请求权限
          for (const perm of permissions) {
            const hasPerm = await new Promise((resolve) => {
              permision.hasPermission(perm, (result) => {
                resolve(result)
              })
            })

            if (!hasPerm) {
              const granted = await new Promise((resolve) => {
                permision.requestPermission(perm, (result) => {
                  resolve(result === 1)
                })
              })

              if (!granted) {
                that.showToast('权限被拒绝，无法选择图片')
                return false
              }
            }
          }
          return true
        } catch (e) {
          uni.showToast({
            title: `权限检查错误`,
            icon: 'none'
          })
          console.error('权限检查错误:', e)
          return true
        }
      }

      // 安卓设备先检查权限
      checkPermissions().then((hasPermission) => {
        if (hasPermission) {
          handleChooseImage()
        } else {
          uni.showModal({
            title: '权限不足',
            content: '需要相册和相机权限才能上传图片，请前往设置开启',
            confirmText: '去设置',
            success: (res) => {
              if (res.confirm) {
                const permision = uni.requireNativePlugin('system.permission')
                permision.gotoAppPermissionSetting()
              }
            }
          })
        }
      })
      // #endif

      // #ifndef APP-PLUS
      // 非安卓设备直接调用
      handleChooseImage()
      // #endif
    },
    selectFile6() {
      uni.showToast({
        title: `点击了选择按钮`,
        icon: 'none'
      })
      let that = this
      if (that.disabled) return

      // 统一处理选择图片逻辑
      const handleChooseImage = async () => {
        const {
          name = '',
          maxCount,
          multiple,
          maxSize,
          sizeType,
          camera,
          compressed,
          maxDuration,
          sourceType
        } = that

        let lists = JSON.parse(JSON.stringify(that.lists))
        const newMaxCount = maxCount - lists.length
        uni.showToast({
          title: `choose前弹出`,
          icon: 'none'
        })
        try {
          // 安卓WebView特殊处理：同步方式调用选择器
          let res = await that.syncChooseImage(
            multiple ? Math.min(newMaxCount, 9) : 1,
            sourceType,
            sizeType
          )

          let listOldLength = that.lists.length
          res.tempFiles.forEach((val, index) => {
            if (!that.checkFileExt(val)) return
            if (!multiple && index >= 1) return

            if (val.size > maxSize) {
              that.$emit('on-oversize', val, that.lists, that.index)
              that.showToast('超出允许的文件大小')
            } else {
              if (maxCount <= lists.length) {
                that.$emit('on-exceed', val, that.lists, that.index)
                that.showToast('超出最大允许的文件个数')
                return
              }
              lists.push({
                url: val.path,
                progress: 0,
                error: false,
                file: val
              })
            }
          })

          that.deepClone(lists, that.lists)
          that.$emit('on-choose-complete', that.lists, that.index)
          if (that.autoUpload) that.uploadFile(listOldLength)
        } catch (error) {
          uni.showToast({
            title: `选择图片失败：${error}`,
            icon: 'none'
          })
          console.error('选择图片失败：', error)
          that.$emit('on-choose-fail', error)
          that.showToast('选择图片失败，请重试')
        }
      }

      // #ifdef APP-PLUS
      // 安卓权限检查 - 仅限APP环境
      const checkPermissions = async () => {
        try {
          const permision = uni.requireNativePlugin('system.permission')
          const permissions = [
            'android.permission.READ_EXTERNAL_STORAGE',
            'android.permission.WRITE_EXTERNAL_STORAGE'
          ]

          if (that.sourceType.includes('camera')) {
            permissions.push('android.permission.CAMERA')
          }

          // 检查并请求权限
          for (const perm of permissions) {
            const hasPerm = await new Promise((resolve) => {
              permision.hasPermission(perm, (result) => {
                resolve(result)
              })
            })

            if (!hasPerm) {
              const granted = await new Promise((resolve) => {
                permision.requestPermission(perm, (result) => {
                  resolve(result === 1)
                })
              })

              if (!granted) {
                that.showToast('权限被拒绝，无法选择图片')
                return false
              }
            }
          }
          return true
        } catch (e) {
          console.error('权限检查错误:', e)
          return true
        }
      }

      // 安卓设备先检查权限
      checkPermissions().then((hasPermission) => {
        if (hasPermission) {
          handleChooseImage()
        } else {
          uni.showModal({
            title: '权限不足',
            content: '需要相册和相机权限才能上传图片，请前往设置开启',
            confirmText: '去设置',
            success: (res) => {
              if (res.confirm) {
                const permision = uni.requireNativePlugin('system.permission')
                permision.gotoAppPermissionSetting()
              }
            }
          })
        }
      })
      // #endif

      // #ifndef APP-PLUS
      // 非安卓设备直接调用
      handleChooseImage()
      // #endif
    },
    // 安卓WebView特殊处理：同步方式调用选择器
    syncChooseImage(count, sourceType, sizeType) {
      uni.showToast({
        title: `进入选择前`,
        icon: 'none'
      })
      return new Promise((resolve, reject) => {
        // 在安卓WebView中，必须将选择器调用放在setTimeout 0中
        setTimeout(() => {
          try {
            uni.chooseImage({
              count,
              sourceType,
              sizeType,
              success: resolve,
              fail: reject
            })
            uni.showToast({
              title: `进入选择后`,
              icon: 'none'
            })
          } catch (e) {
            uni.showToast({
              title: `选择异常${e}`,
              icon: 'none'
            })
            reject(e)
          }
        }, 0)
      })
    },
    selectFile() {
      uni.showToast({
        title: `点击了选择按钮`,
        icon: 'none'
      })
      let that = this
      if (that.disabled) return

      // 统一处理选择图片逻辑
      const handleChooseImage = async () => {
        const {
          name = '',
          maxCount,
          multiple,
          maxSize,
          sizeType,
          camera,
          compressed,
          maxDuration,
          sourceType
        } = that

        let lists = JSON.parse(JSON.stringify(that.lists))
        const newMaxCount = maxCount - lists.length

        try {
          // 在App的WebView中，使用原生input方式
          let res
          // #ifdef H5
          // 判断是否在App的WebView中
          if (navigator.userAgent.includes('; wv)')) {
            uni.showToast({
              title: `在App的WebView`,
              icon: 'none'
            })
            res = await that.chooseImageViaButton(multiple, newMaxCount)
          } else {
            res = await that.uniChooseImage(
              multiple ? Math.min(newMaxCount, 9) : 1,
              sourceType,
              sizeType
            )
          }
          // #endif

          // #ifndef H5
          // 非H5环境使用uni.chooseImage
          res = await that.uniChooseImage(
            multiple ? Math.min(newMaxCount, 9) : 1,
            sourceType,
            sizeType
          )
          // #endif

          let listOldLength = that.lists.length
          res.tempFiles.forEach((val, index) => {
            if (!that.checkFileExt(val)) return
            if (!multiple && index >= 1) return

            if (val.size > maxSize) {
              that.$emit('on-oversize', val, that.lists, that.index)
              that.showToast('超出允许的文件大小')
            } else {
              if (maxCount <= lists.length) {
                that.$emit('on-exceed', val, that.lists, that.index)
                that.showToast('超出最大允许的文件个数')
                return
              }
              lists.push({
                url: val.path,
                progress: 0,
                error: false,
                file: val
              })
            }
          })

          that.deepClone(lists, that.lists)
          that.$emit('on-choose-complete', that.lists, that.index)
          if (that.autoUpload) that.uploadFile(listOldLength)
        } catch (error) {
          uni.showToast({
            title: `选择图片失败${error}`,
            icon: 'none'
          })
          console.error('选择图片失败：', error)
          that.$emit('on-choose-fail', error)
          that.showToast('选择图片失败，请重试')
        }
      }

      // #ifdef APP-PLUS
      // 安卓权限检查 - 仅限APP环境
      const checkPermissions = async () => {
        try {
          const permision = uni.requireNativePlugin('system.permission')
          const permissions = [
            'android.permission.READ_EXTERNAL_STORAGE',
            'android.permission.WRITE_EXTERNAL_STORAGE'
          ]

          if (that.sourceType.includes('camera')) {
            permissions.push('android.permission.CAMERA')
          }

          // 检查并请求权限
          for (const perm of permissions) {
            const hasPerm = await new Promise((resolve) => {
              permision.hasPermission(perm, (result) => {
                resolve(result)
              })
            })

            if (!hasPerm) {
              const granted = await new Promise((resolve) => {
                permision.requestPermission(perm, (result) => {
                  resolve(result === 1)
                })
              })

              if (!granted) {
                that.showToast('权限被拒绝，无法选择图片')
                return false
              }
            }
          }
          return true
        } catch (e) {
          console.error('权限检查错误:', e)
          return true
        }
      }

      // 安卓设备先检查权限
      checkPermissions().then((hasPermission) => {
        if (hasPermission) {
          handleChooseImage()
        } else {
          uni.showModal({
            title: '权限不足',
            content: '需要相册和相机权限才能上传图片，请前往设置开启',
            confirmText: '去设置',
            success: (res) => {
              if (res.confirm) {
                const permision = uni.requireNativePlugin('system.permission')
                permision.gotoAppPermissionSetting()
              }
            }
          })
        }
      })
      // #endif

      // #ifndef APP-PLUS
      // 非安卓设备直接调用
      handleChooseImage()
      // #endif
    },

    // uni.chooseImage的Promise封装
    uniChooseImage(count, sourceType, sizeType) {
      uni.showToast({
        title: `使用chooseImage`,
        icon: 'none'
      })
      return new Promise((resolve, reject) => {
        uni.chooseImage({
          count,
          sourceType,
          sizeType,
          success: resolve,
          fail: reject
        })
      })
    },

    // 安卓WebView特殊处理：通过创建临时按钮触发选择
    chooseImageViaButton(multiple, maxFiles) {
      return new Promise((resolve, reject) => {
        try {
          uni.showToast({
            title: `使用临时input`,
            icon: 'none'
          })
          // 创建临时input元素
          const input = document.createElement('input')
          input.type = 'file'
          input.accept = 'image/*'
          input.style.display = 'none'

          // 设置选择数量限制
          if (multiple) {
            input.multiple = true
            // 最多可以选择maxFiles张
            if (maxFiles > 1) {
              input.setAttribute('max', maxFiles)
            }
          }

          // 处理文件选择
          input.onchange = (e) => {
            const files = Array.from(e.target.files || [])
            if (files.length === 0) {
              reject(new Error('未选择文件'))
              return
            }

            const tempFiles = files.map((file) => ({
              path: URL.createObjectURL(file),
              size: file.size,
              name: file.name,
              type: file.type
            }))

            resolve({ tempFiles })
            document.body.removeChild(input)
          }

          // 添加到DOM并触发点击
          document.body.appendChild(input)
          input.click()

          // 超时处理
          setTimeout(() => {
            if (document.body.contains(input)) {
              document.body.removeChild(input)
              reject(new Error('选择超时'))
            }
          }, 30000)
        } catch (error) {
          reject(error)
        }
      })
    },
    // 提示用户消息
    showToast(message, force = false) {
      if (this.showTips || force) {
        uni.showToast({
          title: message,
          icon: 'none'
        })
      }
    },
    // 该方法供用户通过ref调用，手动上传
    upload() {
      this.uploadFile()
    },
    // 对失败的图片重新上传
    retry(index) {
      this.lists[index].progress = 0
      this.lists[index].error = false
      this.lists[index].response = null
      uni.showLoading({
        title: '重新上传'
      })
      this.uploadFile(index)
    },
    // 上传图片
    async uploadFile(index = 0) {
      if (this.disabled) return
      if (this.uploading) return
      // 全部上传完成
      if (index >= this.lists.length) {
        this.$emit('on-uploaded', this.lists, this.index)
        return
      }
      // 检查是否是已上传或者正在上传中
      if (this.lists[index].progress == 100) {
        if (this.autoUpload == false) this.uploadFile(index + 1)
        return
      }
      // 执行before-upload钩子
      if (this.beforeUpload && typeof this.beforeUpload === 'function') {
        // 执行回调，同时传入索引和文件列表当作参数
        // 在微信，支付宝等环境(H5正常)，会导致父组件定义的customBack()函数体中的this变成子组件的this
        // 通过bind()方法，绑定父组件的this，让this.customBack()的this为父组件的上下文
        // 因为upload组件可能会被嵌套在其他组件内，比如u-form，这时this.$parent其实为u-form的this，
        // 非页面的this，所以这里需要往上历遍，一直寻找到最顶端的$parent，这里用了this.$u.$parent.call(this)
        // 明白意思即可，无需纠结this.$u.$parent.call(this)的细节
        let beforeResponse = this.beforeUpload.bind(this.$u.$parent.call(this))(
          index,
          this.lists
        )
        // 判断是否返回了promise
        if (!!beforeResponse && typeof beforeResponse.then === 'function') {
          await beforeResponse
            .then((res) => {
              // promise返回成功，不进行动作，继续上传
            })
            .catch((err) => {
              // 进入catch回调的话，继续下一张
              return this.uploadFile(index + 1)
            })
        } else if (beforeResponse === false) {
          // 如果返回false，继续下一张图片的上传
          return this.uploadFile(index + 1)
        } else {
          // 此处为返回"true"的情形，这里不写代码，就跳过此处，继续执行当前的上传逻辑
        }
      }
      // 检查上传地址
      if (!this.action) {
        this.showToast('请配置上传地址', true)
        return
      }
      this.lists[index].error = false
      this.uploading = true
      // 创建上传对象
      // #ifdef MP-WEIXIN
      const task = uni.uploadFile({
        url: this.action,
        filePath: this.lists[index].url,
        name: this.name,
        formData: this.formData,
        header: this.header,
        success: (res) => {
          // 判断是否json字符串，将其转为json格式
          let data =
            this.toJson && this.$u.test.jsonString(res.data)
              ? JSON.parse(res.data)
              : res.data
          if (![200, 201, 204].includes(res.statusCode)) {
            this.uploadError(index, data)
          } else {
            // 上传成功
            this.lists[index].response = data
            this.lists[index].progress = 100
            this.lists[index].error = false
            this.$emit('on-success', data, index, this.lists, this.index)
          }
        },
        fail: (e) => {
          this.uploadError(index, e)
        },
        complete: (res) => {
          uni.hideLoading()
          this.uploading = false
          this.uploadFile(index + 1)
          this.$emit('on-change', res, index, this.lists, this.index)
        }
      })
      task.onProgressUpdate((res) => {
        if (res.progress > 0) {
          this.lists[index].progress = res.progress
          this.$emit('on-progress', res, index, this.lists, this.index)
        }
      })
      // #endif
      // #ifdef H5
      try {
        const file = await imageUrlToFile(this.lists[index].url)
        const client = await getAliOss()
        const that = this
        client
          .multipartUpload(
            'uploads/' + generateId() + get_suffix(this.lists[index].file.name),
            file,
            {
              progress(p) {
                if (p * 100 > 0) {
                  that.lists[index].progress = p * 100
                  that.$emit('on-progress', {}, index, that.lists, that.index)
                }
              }
            }
          )
          .then((res) => {
            console.log(res)
            const data = { data: res.name }
            if (res.res.statusCode === 200) {
              this.lists[index].response = data
              this.lists[index].progress = 100
              this.lists[index].error = false
              this.$emit('on-success', data, index, this.lists, this.index)
            } else {
              this.uploadError(index, res)
            }
          })
          .finally(() => {
            uni.hideLoading()
            this.uploading = false
          })
          .catch((err) => {
            // this.uploadError(index, err);
          })
      } catch (err) {
        uni.hideLoading()
        this.uploading = false
        this.uploadError(index, err)
      }
      // #endif
    },
    // 上传失败
    uploadError(index, err) {
      this.lists[index].progress = 0
      this.lists[index].error = true
      this.lists[index].response = null
      this.$emit('on-error', err, index, this.lists, this.index)
      this.showToast('上传失败，请重试')
    },
    // 删除一个图片
    deleteItem(index) {
      uni.showModal({
        title: '提示',
        content: '您确定要删除此项吗？',
        success: async (res) => {
          if (res.confirm) {
            // 先检查是否有定义before-remove移除前钩子
            // 执行before-remove钩子
            if (this.beforeRemove && typeof this.beforeRemove === 'function') {
              // 此处钩子执行 原理同before-remove参数，见上方注释
              let beforeResponse = this.beforeRemove.bind(
                this.$u.$parent.call(this)
              )(index, this.lists)
              // 判断是否返回了promise
              if (
                !!beforeResponse &&
                typeof beforeResponse.then === 'function'
              ) {
                await beforeResponse
                  .then((res) => {
                    // promise返回成功，不进行动作，继续上传
                    this.handlerDeleteItem(index)
                  })
                  .catch((err) => {
                    // 如果进入promise的reject，终止删除操作
                    this.showToast('已终止移除')
                  })
              } else if (beforeResponse === false) {
                // 返回false，终止删除
                this.showToast('已终止移除')
              } else {
                // 如果返回true，执行删除操作
                this.handlerDeleteItem(index)
              }
            } else {
              // 如果不存在before-remove钩子，
              this.handlerDeleteItem(index)
            }
          }
        }
      })
    },
    // 执行移除图片的动作，上方代码只是判断是否可以移除
    handlerDeleteItem(index) {
      // 如果文件正在上传中，终止上传任务，进度在0 < progress < 100则意味着正在上传
      if (this.lists[index].progress < 100 && this.lists[index].progress > 0) {
        typeof this.lists[index].uploadTask != 'undefined' &&
          this.lists[index].uploadTask.abort()
      }
      this.lists.splice(index, 1)
      this.$forceUpdate()
      this.$emit('on-remove', index, this.lists, this.index)
      //this.showToast('移除成功');
    },
    // 用户通过ref手动的形式，移除一张图片
    remove(index) {
      // 判断索引的合法范围
      if (index >= 0 && index < this.lists.length) {
        this.lists.splice(index, 1)
        this.$emit('on-list-change', this.lists, this.index)
      }
    },
    // 预览图片
    doPreviewImage(url, index) {
      if (!this.previewFullImage) {
        this.$emit('on-preview', url, this.lists, this.index)
        return
      }
      const images = this.lists.map((item) => item.url || item.path)
      uni.previewImage({
        urls: images,
        current: url,
        success: () => {
          this.$emit('on-preview', url, this.lists, this.index)
        },
        fail: () => {
          uni.showToast({
            title: '预览图片失败',
            icon: 'none'
          })
        }
      })
    },
    // 判断文件后缀是否允许
    checkFileExt(file) {
      // 检查是否在允许的后缀中
      let noArrowExt = false
      // 获取后缀名
      let fileExt = ''
      const reg = /.+\./
      // 如果是H5，需要从name中判断
      // #ifdef H5
      fileExt = file.name.replace(reg, '').toLowerCase()
      // #endif
      // 非H5，需要从path中读取后缀
      // #ifndef H5
      fileExt = file.path.replace(reg, '').toLowerCase()
      // #endif
      // 使用数组的some方法，只要符合limitType中的一个，就返回true
      noArrowExt = this.limitType.some((ext) => {
        // 转为小写
        return ext.toLowerCase() === fileExt
      })
      if (!noArrowExt) this.showToast(`不允许选择${fileExt}格式的文件`)
      return noArrowExt
    },
    // 深拷贝
    deepClone(obj, newObj) {
      for (let k in obj) {
        const value = obj[k]

        if (Array.isArray(value)) {
          newObj[k] = []
          this.deepClone(value, newObj[k])
        } else if (value !== null && typeof value === 'object') {
          newObj[k] = {}
          this.deepClone(value, newObj[k])
        } else {
          newObj[k] = value
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
// 定义混入指令，用于在非nvue环境下的flex定义，因为nvue没有display属性，会报错
@mixin vue-flex($direction: row) {
  /* #ifndef APP-NVUE */
  display: flex;
  flex-direction: $direction;
  /* #endif */
}

.u-upload {
  @include vue-flex;
  flex-wrap: wrap;
  align-items: center;
}

.u-list-item {
  width: 200rpx;
  height: 200rpx;
  overflow: hidden;
  margin: 10rpx;
  background: rgb(244, 245, 246);
  position: relative;
  border-radius: 10rpx;
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  align-items: center;
  justify-content: center;
}

.u-preview-wrap {
  border: 1px solid rgb(235, 236, 238);
}

.u-add-wrap {
  flex-direction: column;
  color: $u-content-color;
  font-size: 26rpx;
}

.u-add-tips {
  margin-top: 20rpx;
  line-height: 40rpx;
}

.u-add-wrap__hover {
  background-color: rgb(235, 236, 238);
}

.u-preview-image {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 10rpx;
}

.u-delete-icon {
  position: absolute;
  top: 6rpx;
  right: 6rpx;
  z-index: 10;
  background-color: $u-type-error;
  border-radius: 100rpx;
  width: 36rpx;
  height: 36rpx;
  @include vue-flex;
  align-items: center;
  justify-content: center;
}

.u-icon {
  @include vue-flex;
  align-items: center;
  justify-content: center;
}

.u-success-icon {
  position: absolute;
  bottom: 6rpx;
  right: 6rpx;
  z-index: 10;
  background-color: #5ac725;
  border-radius: 100rpx;
  width: 36rpx;
  height: 36rpx;
  @include vue-flex;
  align-items: center;
  justify-content: center;
}

.u-progress {
  position: absolute;
  bottom: 10rpx;
  left: 8rpx;
  right: 8rpx;
  z-index: 9;
  width: auto;
}

.u-error-btn {
  color: #ffffff;
  background-color: $u-type-error;
  font-size: 20rpx;
  padding: 4px 0;
  text-align: center;
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 9;
  line-height: 1;
}
</style>
